# -*- coding: utf-8 -*-

# pip install xlwt
import xlwt


def write_excel():
    """写入excel文件"""

    # 创建新的workbook（其实就是创建新的excel）
    workbook = xlwt.Workbook(encoding='ascii')

    # 创建新的sheet表
    sheet = workbook.add_sheet("sheet1")

    # 初始化样式
    style = xlwt.XFStyle()

    # 创建字体
    font = xlwt.Font()
    # 字体
    font.name = 'Times New Roman'
    # 加粗
    font.bold = True
    # 下划线
    font.underline = True
    # 斜体
    font.italic = True

    # 样式设置字体
    style.font = font

    # 往表格写入内容
    sheet.write(0, 0, "value1")
    # sheet.write(2, 1, "value2")

    # 创建模式
    pattern = xlwt.Pattern()
    # NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
    # 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan,
    # 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow ,
    # 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray
    pattern.pattern_fore_colour = 5
    style = xlwt.XFStyle()
    style.pattern = pattern

    # 创建对齐
    alignment = xlwt.Alignment()
    # VERT_TOP = 0x00       上端对齐
    # VERT_CENTER = 0x01    居中对齐（垂直方向上）
    # VERT_BOTTOM = 0x02    低端对齐
    # HORZ_LEFT = 0x01      左端对齐
    # HORZ_CENTER = 0x02    居中对齐（水平方向上）
    # HORZ_RIGHT = 0x03     右端对齐
    alignment.horz = 0x02  # 设置水平居中
    alignment.vert = 0x01  # 设置垂直居中
    style.alignment = alignment

    # 使用模式
    sheet.write(2, 1, "2", style)

    # 合并 第1行到第2行 的 第0列到第3列
    # sheet.write_merge(1, 2, 0, 3, 'merge1203')

    # 创建边框
    borders = xlwt.Borders()

    # NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR,
    # MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED,
    # MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
    # NO_LINE没有
    # DASHED虚线
    # THIN实线
    # 设置边框线条
    borders.left = xlwt.Borders.DASHED
    borders.right = xlwt.Borders.DASHED
    borders.top = xlwt.Borders.DASHED
    borders.bottom = xlwt.Borders.DASHED

    # 设置边框颜色
    borders.left_colour = 0x40
    borders.right_colour = 0x40
    borders.top_colour = 0x40
    borders.bottom_colour = 0x40

    # 创建样式
    style = xlwt.XFStyle()
    # 样式设置边框
    style.borders = borders

    # 设置列宽
    sheet.col(0).width = 256 * 20

    # 设置行高
    style = xlwt.easyxf('font:height 360;')  # 18pt,类型小初的字号
    row = sheet.row(0)
    row.set_style(style)

    # 保存xls文件（支持xls格式）
    workbook.save("xls.xls")

    pass
